﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class IntentQuestionSelectedKeywordDomainService : IIntentQuestionSelectedKeywordDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, IntentQuestionSelectedKeyword> _repository;

        public IntentQuestionSelectedKeywordDomainService(IRepository<Guid, IntentQuestionSelectedKeyword> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public IntentQuestionSelectedKeyword Create(IntentQuestionSelectedKeyword intentQuestionSelectedKeyword)
        {
            if (intentQuestionSelectedKeyword.Id == Guid.Empty)
            {
                intentQuestionSelectedKeyword.Id = Guid.NewGuid();
            }
            return _repository.Create(intentQuestionSelectedKeyword);
        }

        public IntentQuestionSelectedKeyword Get(Guid id)
        {
            return _repository.Get(id);
        }

        public IntentQuestionSelectedKeyword Update(IntentQuestionSelectedKeywordUpdateBo bo)
        {
            IntentQuestionSelectedKeyword intentQuestionSelectedKeyword = _repository.Get(bo.Id);
            intentQuestionSelectedKeyword.Id = bo.Id;
            intentQuestionSelectedKeyword.QuestionId = bo.QuestionId;
            intentQuestionSelectedKeyword.EntityId = bo.EntityId;
            intentQuestionSelectedKeyword.EntityLabel = bo.EntityLabel;
            intentQuestionSelectedKeyword.StartPosition = bo.StartPosition;
            intentQuestionSelectedKeyword.EndPosition = bo.EndPosition;

            _repository.Update(intentQuestionSelectedKeyword);

            return intentQuestionSelectedKeyword;

        }

        public IEnumerable<IntentQuestionSelectedKeyword> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            IntentQuestionSelectedKeyword intentQuestionSelectedKeyword = _repository.Get(id);

            _repository.Delete(intentQuestionSelectedKeyword);

        }

        public IEnumerable<IntentQuestionSelectedKeyword> GetByQuestionId(Guid questionId, string include = "")
        {
            var list = _repository.List(new IntentQuestionSelectedKeywordFilterSpecification(questionId)).ToList();
            if(list!=null&& list.Count>0 &&!string.IsNullOrEmpty(include)&& include.Split(',').Contains("entity"))
            {
                IEntityDomainService entityDomainService = _container.Resolve<IEntityDomainService>();
                foreach (var item in list)
                {
                    if(item.EntityId != Guid.Empty)
                    {
                        item.Entity = entityDomainService.Get(item.EntityId);
                    }
                }
            }
            return list;
        }
    }
}
